<?php

class UserAction extends CommonAction {

	public function index()
	{
		$this->assign('__JSLIB__',__ROOT__ .'/'.APP_PATH.'/public/js/');
		$this->display();
	}
	public function data()
	{
		$u = new UserModel();
		switch($_POST['do'])
		{
			case 'did':
				$d = new DepartmentModel();
				echo $d->getlist_user('name,did');
				break;
			case 'roleid':
				$r = new RoleModel();
				echo json_encode($r->field('name,id')->select());
				break;
			case 'userid':
				$u = new UserModel();
				$ur = D('RoleUser');
				$map['id'] = $_POST['where'];
				$rolemap['user_id'] = $map['id'];
				$array = $u->where($map)->field('account,id,did,nickname,email,type_id as datatype,remark as info')->find();
				$array['roleid'] = $ur->where($rolemap)->getField('role_id');
				echo json_encode($array);
				break;
			case 'type':
				$userinfo = Session::get('userinfo');
				switch($userinfo['type_id']){
					case 1:
						$map = '';
					break;
					case 2:
						$usermap['did'] = $userinfo['did'];
						$userObj = new UserModel();
						foreach($userObj->where($usermap)->select() as $v)
						{
							$ids[] = $v['id'];
						}
						$map['id'] = array('in',$ids);
					break;
					case 3:
						$map['id'] = $userinfo['id'];
					break;
				}
				echo json_encode($u->where($map)->field('nickname as name,id')->select());
			break;
			default:
				echo $u->getlist('*',$_POST['page'],$_POST['limit']);
			break;
		}

	}
    // 更换密码
    public function changePwd()
    {

        //对表单提交处理进行处理或者增加非表单数据
		if(md5($_POST['verify'])	!= $_SESSION['verify']) {
			$this->error('验证码错误！');
		}
		$map	=	array();
        $map['password']= pwdHash($_POST['oldpassword']);
        if(isset($_POST['account'])) {
            $map['account']	 =	 $_POST['account'];
        }elseif(isset($_SESSION[C('USER_AUTH_KEY')])) {
            $map['id']		=	$_SESSION[C('USER_AUTH_KEY')];
        }
        //检查用户
        $User    =   M("User");
        if(!$User->where($map)->field('id')->find()) {
            $this->error('旧密码不符或者用户名错误！');
        }else {
			$User->password	=	pwdHash($_POST['password']);
			$User->save();
			$this->success('密码修改成功！');
         }
    }
	public function profile() {

		$User = M("User");
		$vo	= $User->getById($_SESSION[C('USER_AUTH_KEY')]);
		$this->assign('vo',$vo);
		$this->display();
	}

// 修改资料
	public function change() {
		$User = D("User");
		if(!$User->create()) {
			$this->error($User->getError());
		}
		$result	=	$User->save();
		if(false !== $result) {
			$this->success('资料修改成功！');
		}else{
			$this->error('资料修改失败!');
		}
	}
	public function add() {
		if($this->isPost())
		{
			$method = $_POST;
			$data = array();
			$userObj = new UserModel();
			$roleObj = D('RoleUser');
			$data['account'] = $method['account'];
			$data['password'] = md5($method['password']);
			$data['nickname'] = $method['nickname'];
			$data['email'] = $method['email'];
			$data['did'] = $method['did'];
			$data['create_time'] = time();
			$data['status'] = 1;
			$data['type_id'] = $method['datatype'];
			$role['role_id'] = $method['roleid'];
			$data['remark'] = $method['info'];
			if($userid = $userObj->add($data)){
				$role['user_id'] = $userid;
				$roleObj->add($role);
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}
	}
	public function edit() {
		if($this->isPost())
		{
			$method = $_POST;
			$data = array();
			$userObj = new UserModel();
			$ruObj = D('RoleUser');
			$userObj->id = $method['id'];
			if($method['password'] != null) $userObj->password = md5($method['password']);
			$userObj->nickname = $method['nickname'];
			$userObj->email = $method['email'];
			$userObj->did = $method['did'];
			$userObj->type_id = $method['datatype'];
			$userObj->remark = $method['info'];
			$userObj->update_time = time();
			$rudata['role_id'] = $method['roleid'];
			$ru['user_id'] = $method['id'];
			if($userObj->save()){
				$ruObj->data($rudata)->where($ru)->save();
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}
	}
	public function status(){
		if($this->isPost())
		{
			$method = json_decode(file_get_contents("php://input"))->list;
			$userObj = new UserModel();
			$userObj->id = $method->id;
			$userObj->status = $method->status;
			if($userObj->save()){
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}
	}
	public function delete() {
		if($this->isPost())
		{
			$method = json_decode(file_get_contents("php://input"))->list;
			$data = array();
			$userObj = new UserModel();
			$ruObj = D('RoleUser');
			$map['id'] = $method;
			$rumap['user_id'] = $map['id'];
			if($userObj->where($map)->delete()){
				$ruObj->where($rumap)->delete();
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
	}
}